/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.shape.annotation.impl;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import who.tmap.bo.cd.CDPointSymbols;
import who.tmap.bo.cd.point.CDPointBasic;
import who.tmap.bo.cd.point.CDPointDataDrivenEntry;
import who.tmap.bo.cd.point.CDPointDataDrivenSize;
import who.tmap.bo.cd.point.CDPointDataDrivenSymbol;
import who.tmap.bo.cd.point.CDPointDataDrivenSymbolAndSize;
import who.tmap.toolkit.shape.annotation.LegendShape;

public class PointLegendShapeHelper {

	private LegendShape legendShape;
	
	public PointLegendShapeHelper( LegendShape legendShape ) {
		this.legendShape =legendShape;
	}
	
	public LegendShape getLegendShape() {
		return legendShape;
	}

	public void setLegendShape(LegendShape legendShape) {
		this.legendShape = legendShape;
	}
	
	protected TextRenderingHelper prepareLegend( Graphics2D g, Point2D legendTextPt, double screenToMapFactor ) {
		TextRenderingHelper trh =new TextRenderingHelper( screenToMapFactor, legendShape.getZoomFactor() );
		
		trh.init( g, legendShape.getFontName(), legendShape.getFontStyle(), legendShape.getFontSize() );			
		
		if ( legendShape.getCartographicDisplay() instanceof CDPointBasic 
			|| legendShape.getCartographicDisplay() instanceof CDPointDataDrivenSize ) {
			Color color =null;
			int fillPattern =0;
			int symbol =CDPointSymbols.DEFAULT;
			String descColumn ="";
			Image img =null;
			
			if ( legendShape.getCartographicDisplay() instanceof CDPointBasic ) {
				CDPointBasic cdpb =(CDPointBasic)legendShape.getCartographicDisplay();
				
				color =cdpb.getColor();
				fillPattern =cdpb.getFillPattern();
				symbol =cdpb.getSymbol();
				descColumn =cdpb.getDescriptorColumn();
				img =cdpb.getImage();
				
			} else if ( legendShape.getCartographicDisplay() instanceof CDPointDataDrivenSize ) {
				CDPointDataDrivenSize cdpdds =(CDPointDataDrivenSize)legendShape.getCartographicDisplay();
				
				color =cdpdds.getColor();
				fillPattern =cdpdds.getFillPattern();
				symbol =cdpdds.getSymbol();
				descColumn =cdpdds.getDescriptorColumn();
				img =cdpdds.getImage();
			}
			
			trh.prepareTextForRendering( g, descColumn, legendTextPt, TextRenderingHelper.NO_MARGINS );
			legendShape.getLegendSamples().add( 
						new LegendSample( new Point2D.Double( legendShape.getPt().getX(), legendTextPt.getY() + trh.getYOffsetForLine( 0 ) )
						   	 , color
						     , legendShape.getTexturePaint( fillPattern )
						     , symbol, img ) );			
			
		} else if ( legendShape.getCartographicDisplay() instanceof CDPointDataDrivenSymbol 
					|| legendShape.getCartographicDisplay() instanceof CDPointDataDrivenSymbolAndSize ) {
			Map<String, CDPointDataDrivenEntry> entries =null;
			String descColumn ="";
			
			if ( legendShape.getCartographicDisplay() instanceof CDPointDataDrivenSymbol ) {
				entries =((CDPointDataDrivenSymbol)legendShape.getCartographicDisplay()).getEntries();
				descColumn =((CDPointDataDrivenSymbol)legendShape.getCartographicDisplay()).getDataColumn();
			} else if ( legendShape.getCartographicDisplay() instanceof CDPointDataDrivenSymbolAndSize ) {
				entries =((CDPointDataDrivenSymbolAndSize)legendShape.getCartographicDisplay()).getEntries();
				descColumn =((CDPointDataDrivenSymbolAndSize)legendShape.getCartographicDisplay()).getDataColumn();				
			}
			
			List<String> lines =new ArrayList<String>();	
			
			if ( entries != null ) {
			
				int entryCt =0;
				for( CDPointDataDrivenEntry entry : entries.values() ) {
					lines.add( entry.getVal() );
					
					legendShape.getLegendSamples().add( 
							new LegendSample( new Point2D.Double( legendShape.getPt().getX(), legendTextPt.getY() + trh.getYOffsetForLine( entryCt++ ) )
							   , entry.getColor()
							   , legendShape.getTexturePaint( entry.getFillPattern() )
							   , entry.getSymbol(), entry.getImage() ) );										
				}
				
			}		
			
			trh.prepareTextForRendering( g, lines, legendTextPt, TextRenderingHelper.NO_MARGINS );
		}
		
		return trh;
	}	

}
